package com.example.testlibraryproject.helpers;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.example.testlibraryproject.callbacks.OnItemSwipeCallback;
import com.example.testlibraryproject.utils.L;

/**
 * To be used to intercept swipe listeners on activity
 * 
 * @author Dayanand
 *
 */
public class SwipeGestureListener extends GestureDetector.SimpleOnGestureListener {
	
	public static final int SWIPE_TYPE_LEFT = 1;
	public static final int SWIPE_TYPE_RIGHT = 2;
	public static final int SWIPE_TYPE_BOTTOM = 3;
	public static final int SWIPE_TYPE_TOP = 4;
	
	private static final int SWIPE_DISTANCE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;
		
	OnItemSwipeCallback mOnItemSwipeListener;
	
    private Context context;

    public SwipeGestureListener(Context applicationContext) {
		this.context = applicationContext;
	}
    
	public SwipeGestureListener(){
		
	}
	
	public OnItemSwipeCallback getmOnItemSwipeListener() {
		return mOnItemSwipeListener;
	}
	
	public void setOnItemSwipeListener(OnItemSwipeCallback mOnItemSwipeListener) {
		this.mOnItemSwipeListener = mOnItemSwipeListener;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		L.v("Flinged. EVENTX1="+e1.getX()+" EVENTX2="+e2.getX()+" EVENTY1="+e1.getY()+" EVENTY2="+e2.getY()+" VelocityX="+velocityX+" VelocityY="+velocityY);
		try {
			// swipe right if final X coordinate is smaller than initial X coordinate
			// - and velocity in X direction is greater than velocity in Y direction.
			float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                    	//swipe right
                        mOnItemSwipeListener.onItemSwipe(SWIPE_TYPE_RIGHT);
                        return true;
                    } else {
                    	//swipe left
                    	mOnItemSwipeListener.onItemSwipe(SWIPE_TYPE_LEFT);
                        return true;
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                    	//swipe bottom
                        mOnItemSwipeListener.onItemSwipe(SWIPE_TYPE_BOTTOM);
                        return true;
                        
                    } else {
                        //swipe top
                        mOnItemSwipeListener.onItemSwipe(SWIPE_TYPE_TOP);
                        return true;
                    }
                }
            }

        } catch (Exception e) {
            L.v("MyGestureDetector onFling", e.toString());
        }
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}
}
